<?php

/**
 * 病友圈表
 */

namespace app\common\model;

use MongoDB\BSON\ObjectId;
use app\common\lib\ZhyxMongo;
use catcher\exceptions\FailedException;

class WardmateCircle extends BaseModel
{
    protected $name = "wardmate_circle";


    /**
     * 获取列表
     * @param array $where 查询条件
     * @param string $field 字段
     */
    public function getLists(array $where = [], string $field = "*")
    {

        return $this->where($where)->field($field)->order('created_at', 'desc')->paginate($this->pageLimit)->append(["new_message"]);
    }

    /**
     * 条件获取数据
     * @param array $where 查询条件
     * @param string $field 字段
     */
    public function getOneData(array $where = [], string $field = "*")
    {
        return $this->where($where)->field($field)->find();
    }

    /**
     * 添加数据
     * @param array $param 参数
     * @return string
     */
    public function addData(array $param = []): string
    {
        //添加时销毁自带的主键id
        unset($param['id']);
        return $this->save($param);
    }

    /**
     * 更新数据
     * @param string $id 主键
     * @param array $param 参数
     * @return bool
     */
    public function saveData(string $id, array $param = []): bool
    {
        $model = self::find($id);
        return $model->save($param);
    }

    /**
     * 删除数据
     * @param string $id
     * @return bool
     */
    public function delData(string $id): bool
    {
        return self::destroy($id);
    }

    /**
     * mongo追加新元素到array类型字段
     * @param string $id 主键
     * @param string $field 字段
     * @param array $data 参数
     * @return bool
     */
    public function appToData(string $id, string $field, array $data = []): bool
    {
        try {
            ZhyxMongo::getInstance()->setCollection($this->name)->updateMany(["_id" => new ObjectId($id)], ['$push' => [$field => ['$each' => $data]]]);
            return true;
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }

    /**
     * mongo移除array类型字段里的元素
     * @param string $id 主键
     * @param string $field 字段
     * @param string $key 键
     * @param string $value 值
     * @return bool
     */
    public function removeToData(string $id, string $field, string $key, string $value): bool
    {
        try {
            ZhyxMongo::getInstance()->setCollection($this->name)->updateMany(["_id" => new ObjectId($id)], ['$pull' => [$field => [$key => $value]]]);
            return true;
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }

    public function getNewMessageAttr($value, $data)
    {

        $result = (new WardmateCircleContent())->where("group_id", "=", $data["id"])->order("id", "desc")->find();
        if (!empty($result)) {

            return [
                'message' => $result["type"] === 1 ? $result["written"] : "[媒体消息]",
                'create_date' => $result["created_at"]
            ];
        }
        return [];

    }


}